package com.virgo.demo.cms.controller.admin;

import com.virgo.common.entity.IResponse;
import com.virgo.common.entity.ListDataVo;
import com.virgo.demo.cms.domain.bo.ArticleCateBo;
import com.virgo.demo.cms.domain.vo.ArticleCateVo;
import com.virgo.demo.cms.service.ArticleCateService;
import com.virgo.demo.core.annotation.Log;
import com.virgo.demo.core.consts.SiteConst;
import com.virgo.limit.annotation.RepeatSubmit;
import com.virgo.web.utils.IRequestUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/manage168/articleCate")
@RequiredArgsConstructor
public class  ArticleCateController {

	private final ArticleCateService articleCateService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@Log(module = "文章分类", type = "编辑", permission = "article_cate_save")
	@RepeatSubmit
	public IResponse<Void> saveAction(@Validated ArticleCateBo articleCateBo) {
		articleCateService.saveArticleCate(articleCateBo);
		return IResponse.ok();
	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@Log(module = "文章分类", type = "删除", permission = "article_cate_del")
	@RepeatSubmit
	public IResponse<Void> delAction(HttpServletRequest request) {
		String codes = IRequestUtils.getString(request,"codes","");
		if (ObjectUtils.isEmpty(codes)) {
			return IResponse.fail("参数错误");
		}
		articleCateService.deleteArticleCate(codes);
		return IResponse.ok();
	}

	@RequestMapping(value = "/status", method = RequestMethod.POST)
	@Log(module = "文章分类", type = "编辑", permission = "article_cate_status")
	@RepeatSubmit
	public IResponse<Void> statusAction(HttpServletRequest request) {
		Long id = IRequestUtils.getLong(request,"id");
		Integer status = IRequestUtils.getInteger(request,"status");
		if (ObjectUtils.isEmpty(id) || id.compareTo(0L) < 1 || !SiteConst.yesOrNoList.contains(status)) {
			return IResponse.fail("参数错误");
		}
		articleCateService.updateStatus(id,status);
		return IResponse.ok();
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@Log(permission = "article_cate_list", saveLog = false)
	public IResponse<ListDataVo<ArticleCateVo>> listAction(HttpServletRequest request) {
		return IResponse.ok(articleCateService.findList(request));
	}
}